package battleship.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class HelpMenu {

	String file;
	String title;
	
	public HelpMenu(String file, String title) {
		this.file = file;
		this.title = title;
	}

	public void init() {
		final JFrame helpMenuFrame = new JFrame(title);
		final JEditorPane editorPane;
		JPanel pnlZoekBalk = new JPanel();

		pnlZoekBalk.setLayout(new FlowLayout());

		editorPane = new JEditorPane();
		editorPane.setEditable(false);

		/*
		 * JOptionPane.showMessageDialog(null,HelpMenu.class.getResource(".").
		 * toString()); File dir=new
		 * File(HelpMenu.class.getResource(".").toString()); String[]
		 * subdirs=dir.list(); String uitvoer=""; for(String subdir:subdirs) {
		 * uitvoer+=subdir + "\n "; }
		 * JOptionPane.showMessageDialog(null,uitvoer);
		 */
		java.net.URL html = null;
		try {
			html = new URL("file://" + System.getProperty("user.dir")
					+ "/help/" + file);
		} catch (MalformedURLException ex) {
			System.err.println("Bestand kon niet gevonden worden");
			ex.printStackTrace();
		}

		if (html != null) {
			try {
				editorPane.setPage(html);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.err.println("Couldn't find file: " + file);
		}
		JScrollPane helpContainer = new JScrollPane(editorPane);
		helpContainer
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		helpContainer.setPreferredSize(new Dimension(250, 145));
		helpContainer.setMinimumSize(new Dimension(10, 10));

		helpMenuFrame.setLayout(new BorderLayout());
		helpMenuFrame.getContentPane().add(pnlZoekBalk, BorderLayout.NORTH);
		helpMenuFrame.getContentPane().add(helpContainer,
				BorderLayout.CENTER);

		helpMenuFrame.setSize(500, 350);
		helpMenuFrame.setVisible(true);
	}

}
